package com.foyekoo.project001.utils;

import android.app.Application;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import com.foyekoo.project001.App;
import com.foyekoo.project001.R;

public class SoundUtil {

	private static SoundPool pool;

	/**
	 * The sound of the start
	 */
	public static int start;
	/**
	 * The sound of the end
	 */
	public static int end;
	/**
	 * The sound of the wrong
	 */
	public static int wrong;
	/**
	 * The sound of join to team
	 */
	public static int confirm;
	/**
	 * The sound of reject to team
	 */
	public static int reject;
	/**
	 * The sound of exit the team
	 */
	public static int exit;
	/**
	 * The sound of receive the invitation
	 */
	public static int invite;

	public static int dingdong;
	
	public static boolean isPlay = true;

	static void init() {
		try {
			if(pool != null)
				pool.release();
			Application app = App.getInstance();
			pool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
			pool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
				@Override
				public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
					Log.d("acthome","load is ok");
				}
			});
			start = pool.load(app, R.raw.music_01, 1);

		} catch(Exception e){
			Log.d("ActHome","load exception");
			e.printStackTrace();
		}
	}
	
	/**
	 * Play the sound
	 * 
	 * @param action
	 *            the tag of the sound
	 */
	public static void playSound(int action) {
		isPlay = true;
		try {
			Log.d("acthome","action-->" + action);
			if (!isPlay) return;
			if (pool == null) {
				init();
			}
	
			// 初始化
			if (action == -100)
				return;
	
			// 连续试播三次，使用成功播放的机会加大
			int res = pool.play(action, 1, 1, 1024, 0, 1);
			if (res == 0) {
				Log.d("acthome","first");
				res = pool.play(action, 1, 1, 1024, 0, 1);
			}
			if (res == 0) {
				res = pool.play(action, 1, 1, 1024, 0, 1);
			}
			Log.d("acthome","play result--" + res);
		} catch(Exception e){
			Log.d("Acthome","--> play Exception" );
			e.printStackTrace();
			init();
		}
	}
}
